class Catalogo::EntradasController < ApplicationController

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @instances = EntradasCatalogo.paginate :page => params[:page], :per_page => DEFAULT_PER_PAGE, :order => 'alternate_id', :conditions => {:catalogo_id => params[:id]}

  end

  def new
    @entrada = EntradasCatalogo.new
#    @catalogo_parent = Catalogo.find(:all, { :order => :nombre })
    p_v_id = Catalogo.find(params[:id]).parent_id
    @catalogo_parent = Array.new
    @catalogo_parent = EntradasCatalogo.find_all_by_catalogo_id(p_v_id) unless p_v_id.blank?
    @id = params[:id]

  end

  def create
    @entrada = EntradasCatalogo.new(params[:entrada])
    if @entrada.save
      flash[:notice] = "El Valor del Catalogo se agrego correctamente."
      redirect_to :action => "list", :id => @entrada.catalogo_id

    else
      p_v_id = Catalogo.find(params[:id]).parent_id unless params[:id].blank?
      @catalogo_parent = Array.new
      @catalogo_parent = EntradasCatalogo.find_all_by_catalogo_id(p_v_id) unless p_v_id.blank?
      render :action => 'new'

    end

  end

  def update
    @entrada = EntradasCatalogo.find(params[:id])
    
    if @entrada.update_attributes(params[:entrada])
      flash[:notice] = "El Valor del Catalogo se actualizo correctamente."
      redirect_to :action => 'list', :id => @entrada.catalogo_id
    else
      render :action => 'edit'
    end

  end

  def edit
    @entrada = EntradasCatalogo.find(params[:id])

    if (@entrada.parent_value_id == 0) || (@entrada.parent_value_id == nil)
      @catalogo_parent = []
    else
      @catalogo_parent = Catalogo.find(EntradasCatalogo.find(@entrada.parent_value_id).catalogo_id).entradas_catalogo
    end

  end
end
